package com.qczl.core.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class DateUtil {
	/**
	 * 把日期字符串格式化成日期类型
	 * 
	 * @param dateStr
	 * @param format
	 * @return
	 */
	public static Date convert2Date(String dateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		try {
			date = sdf.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 获取当天开始时间
	 * 
	 * @return
	 */
	public static Date getStartTime() {
		long current=System.currentTimeMillis();//当前时间毫秒数
		long zero=current/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
		return new Timestamp(zero);
	}

	public static void main(String[] args) {
		System.out.println(getStartTime());
		
//		long current=System.currentTimeMillis();//当前时间毫秒数
//		long zero=current/(1000*3600*24)*(1000*3600*24)-TimeZone.getDefault().getRawOffset();//今天零点零分零秒的毫秒数
//		long twelve=zero+24*60*60*1000-1;//今天23点59分59秒的毫秒数
//		long yesterday=System.currentTimeMillis()-24*60*60*1000;//昨天的这一时间的毫秒数
//		System.out.println(new Timestamp(current));//当前时间
//		System.out.println(new Timestamp(yesterday));//昨天这一时间点
//		System.out.println(new Timestamp(zero));//今天零点零分零秒
//		System.out.println(new Timestamp(twelve));//今天23点59分59秒
	}

	public static Date getEndTime() {
		Calendar todayEnd = Calendar.getInstance();
		todayEnd.set(Calendar.HOUR, 23);
		todayEnd.set(Calendar.MINUTE, 59);
		todayEnd.set(Calendar.SECOND, 59);
		todayEnd.set(Calendar.MILLISECOND, 999);
		return todayEnd.getTime();
	}

	public static String getWeekOfDate(Date dt) {
		String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar cal = Calendar.getInstance();
		cal.setTime(dt);
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
		if (w < 0)
			w = 0;
		return weekDays[w];
	}

}
